/**
 * fileName:  EfficacyReader.java
 * author:  Lei
 * Company:  Cloudland Studio
 * Createdate:  2014-6-3 下午9:25:59
 *
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2011
 *   
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2014-6-3          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.commercial.masterdata.store.read;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.cloudland.commercial.masterdata.store.DatabaseReader;
import org.cloudland.commercial.masterdata.store.entity.EfficacyEntity;
import org.cloudland.commercial.masterdata.store.entity.PageQueryEntity;
import org.cloudland.jdbc.ibatis.masterdata.entity.EfficacyDictEntity;
import org.cloudland.jdbc.ibatis.masterdata.handler.EfficacyDictMapper;
import org.cloudland.jdbc.ibatis.masterdata.handler.condition.Term;
import org.springframework.stereotype.Service;

 /**
 * <pre> 
 * TODO 请充分描述该类提供的作用, 或与其他类会有关联的使用。
 * 使用 java.lang.Object#toString(), 或者 {@link java.lang.Character Character} 等标签
 * </pre>
 * @ClassName  EfficacyReader
 * @author  Lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
@Service
public class EfficacyReader implements DatabaseReader<EfficacyEntity> {

	@Resource(name = "efficacyDictMapper")
	private EfficacyDictMapper efficacyMapper;
	
	/**
	 * <pre>
	 * TODO 请充分描述该构造函数的作用，及其参数的作用。
	 * </pre>
	 *   
	 */
	public EfficacyReader() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * <pre>
	 * TODO 请充分描述该方法提供的功能，及如何使用。
	 * </pre>
	 *
	 * @param id   
	 */
	public EfficacyEntity findEfficacyById(String id) {
		
		EfficacyEntity entity = null;
		EfficacyDictEntity entityDao = efficacyMapper.findById(id);
		if (null != entityDao) {
			entity = new EfficacyEntity(entityDao.getId(), entityDao.getName(), entityDao.getDescribe(), (int) entityDao.getSort());
		}
		return entity;
	}
	
	/**
	 * <pre>
	 * 内部执行方法
	 * </pre>
	 *
	 * @param terms
	 * @return   
	 */
	private PageQueryEntity<EfficacyEntity> findEfficacyByTerm(Map<String, String> terms) {
		
		Term term = new Term();
		term.setName(terms.get("name"));
		term.setBeginRow(Integer.parseInt(terms.get("startNo")));
		term.setPageRow(Integer.parseInt(terms.get("pageSize")));
		
		
		List<EfficacyDictEntity> listDao = efficacyMapper.findPageByTerm(term);
		List<EfficacyEntity> list = new ArrayList<EfficacyEntity>(listDao.size());
		for (EfficacyDictEntity entityDao : listDao) {
			list.add(new EfficacyEntity(entityDao.getId(), entityDao.getName(), entityDao.getDescribe(), (int) entityDao.getSort()));
		}
		int count = efficacyMapper.findCountByTerm(term);
		
		return new PageQueryEntity<EfficacyEntity>(count, list);
	}
	
	/**
	 * <pre>
	 * 按名称分页查询
	 * </pre>
	 *
	 * @param name
	 * @param startNo
	 * @param pageSize
	 * @return   
	 */
	public PageQueryEntity<EfficacyEntity> findEfficacyByName(String name, Integer startNo, Integer pageSize) {
		Map<String, String> terms = new HashMap<String, String>();
		terms.put("name", name);
		terms.put("startNo", String.valueOf(startNo));
		terms.put("pageSize", String.valueOf(pageSize));
		
		return this.findEfficacyByTerm(terms);
	}
	
	/**
	 * <pre>
	 * 分页查询
	 * </pre>
	 *
	 * @param startNo
	 * @param pageSize
	 * @return   
	 */
	public PageQueryEntity<EfficacyEntity> findEfficacyForPage(Integer startNo, Integer pageSize) {
		Map<String, String> terms = new HashMap<String, String>();
		terms.put("startNo", String.valueOf(startNo));
		terms.put("pageSize", String.valueOf(pageSize));
		
		return this.findEfficacyByTerm(terms);
	}
	
	/**
	 * <pre>
	 * 查询全部功效信息
	 * </pre>
	 * @author yong
	 * @date 2014-6-22
	 * @return   
	 */
	public List<EfficacyEntity> findAllEfficacy()
	{
		
		List<EfficacyDictEntity> daoList = efficacyMapper.findAllEfficacyDict();
		if(null == daoList)
		{
			return null;
		}
		List<EfficacyEntity> efficacyList = new ArrayList<EfficacyEntity>(daoList.size());
		for(EfficacyDictEntity daoEntity : daoList)
		{
			efficacyList.add(new EfficacyEntity(daoEntity.getId(), daoEntity.getName(), daoEntity.getDescribe(), Integer.valueOf(daoEntity.getSort())));
		}
		return efficacyList;
	}

}
